package com.heima.common.exception;

/**
 * @author Jt
 * @date 2021/4/10 17:01
 */

import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 *
 */
@Slf4j
@Configuration
@RestControllerAdvice   // Springmvc 异常处理拦截注解
public class ExceptionCatch {

    /**
     * 统一处理异常的方法
     *
     * @return
     */
    @ExceptionHandler(Exception.class)  // exception 所有子类
    public ResponseResult exception(Exception e) {
        // 记录日志
        log.error("服务器出现异常===> {}", e);
        return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR, "服务器出现异常，请稍后重试");
    }


    /**
     * 可预知的异常
     * 拦截自定义异常
     * @param e
     * @return
     */
    @ExceptionHandler(CustomException.class)
    public ResponseResult customException (CustomException e) {
//        记录日志
        log.info("CustomException e ===> {}",e);
        AppHttpCodeEnum codeEnum = e.getAppHttpCodeEnum();
        return ResponseResult.errorResult(codeEnum);
    }

}
